package com.ruoyi.ms.task;

import com.ruoyi.ms.service.IMsPackageService;
import com.ruoyi.xp.order.dto.OrderDto;
import com.ruoyi.xp.order.service.IOrderInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author chendx
 */
@Component("PackageTask")
public class MsPackageTask {

    private static final Logger log = LoggerFactory.getLogger(MsPackageTask.class);

    @Autowired
    private IMsPackageService msPackageService;

    @Autowired
    private IOrderInfoService orderInfoService;

    public void syncMsPackageInfo() throws IOException, InterruptedException {

        log.info("PackageTask.syncMsPackageInfo start");

        msPackageService.syncMsPackageInfo();

        log.info("PackageTask.syncMsPackageInfo end");
    }

    public void syncMsPackageWaitToShip() throws IOException {

        log.info("PackageTask.syncMsPackageUnpaidOrder start");

        List<OrderDto> orderDtoList = orderInfoService.getOrderWaitToShip();
        for (OrderDto orderDto : orderDtoList) {
            msPackageService.syncMsPackageInfoByOrderSn(orderDto.getOrderSn(), orderDto.getOrderStatus());
        }

        log.info("PackageTask.syncMsPackageUnpaidOrder end");
    }

    public void syncMsPackageShipping() throws IOException {

        log.info("PackageTask.syncMsPackageShipping start");

        List<OrderDto> orderDtoList = orderInfoService.getOrderShipping();
        for (OrderDto orderDto : orderDtoList) {
            msPackageService.syncMsPackageInfoByOrderSn(orderDto.getOrderSn(), orderDto.getOrderStatus());
        }

        log.info("PackageTask.syncMsPackageShipping end");
    }

    public void syncMsOrderIncome() throws IOException {
        log.info("PackageTask.syncMsOrderIncome start");

        List<OrderDto> orderDtoList = orderInfoService.getHaveNoShippingFeeOrder();
        for (OrderDto orderDto : orderDtoList) {
            msPackageService.syncMSPackageIncome(orderDto.getOrderSn());
        }

        log.info("PackageTask.syncMsOrderIncome end");
    }
}
